package com.example.jeong.yuejian10.Activity;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.jeong.yuejian10.Bean.AttentionBean;
import com.example.jeong.yuejian10.Bean.IsAttentionBean;
import com.example.jeong.yuejian10.Model.MatchModel;
import com.example.jeong.yuejian10.R;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.jeong.yuejian10.constant.Constant.MATCH_PHOTO_URL;

//比赛详情页
public class MatcthDetailActivity extends Activity {

    private ImageView backlist;
    private ImageView collect;
    private ImageView m_pic;
    private TextView m_time;
    private TextView m_phone;
    private TextView m_name;
    private TextView m_price;
    private TextView m_address;

    private Button baoming;

    private int m_id;
    private int u_id;
    private String coresult="";
    private int flag;
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_macth_detail);
        m_id = getIntent().getIntExtra("m_id", -1);
        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("u_id",0);
        initViews();
        initEvents();
        getMatchData();
        isBeCollected();
    }
    private void initViews(){
        collect = (ImageView) findViewById(R.id.collect);
        backlist = (ImageView) findViewById(R.id.back_matchlist);
        m_name = (TextView) findViewById(R.id.m_name);
        m_address = (TextView) findViewById(R.id.m_address);
        m_time = (TextView) findViewById(R.id.m_time);
        m_phone = (TextView) findViewById(R.id.m_phone);
        m_price = (TextView) findViewById(R.id.m_price);
        baoming = (Button) findViewById(R.id.baoming);
        m_pic = (ImageView) findViewById(R.id.m_pic);
    }
    private void initEvents(){
        //返回列表页
        backlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectMatch();
            }
        });

        m_id=getIntent().getIntExtra("m_id",-1);

        baoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MatcthDetailActivity.this,EnterMatchActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("m_id",m_id);
                //  Log.d("m","mId");
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
            }
        });
    }
    private void getMatchData(){
        Bundle bundle = this.getIntent().getExtras();
        int m_id = bundle.getInt("m_id");
        //  Log.d("m","m_id");
        String mname = bundle.getString("m_name");
        String maddress = bundle.getString("m_address");
        String mphone = bundle.getString("m_phone");
        int mprice = bundle.getInt("m_price");
        String mtime = bundle.getString("m_time");
        String mpic = MATCH_PHOTO_URL+bundle.getString("m_picture");

        m_name.setText(mname);
        m_address.setText(maddress);
        m_phone.setText(mphone);
        m_price.setText(mprice+"");
        m_time.setText(mtime);
        Picasso.with(this).load(mpic).into(m_pic);
    }
    private void isBeCollected(){
        //判断是否关注
        MatchModel matchModel=new MatchModel();
        Call<IsAttentionBean> callIsCollect=matchModel.getIsCollected(u_id,m_id,"2");
        callIsCollect.enqueue(new Callback<IsAttentionBean>() {
            @Override
            public void onResponse(Call<IsAttentionBean> call, Response<IsAttentionBean> response) {
                String result=response.body().getCollected();
                if ("0".equals(result)) {

                    collect.setImageResource(R.drawable.ic_collect_hui);
                    coresult = "no";
                } else {
                    coresult = "yes";
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
            }

            @Override
            public void onFailure(Call<IsAttentionBean> call, Throwable t) {

            }
        });

    }


    //关注大师
    private void collectMatch(){
        MatchModel proModel=new MatchModel();
        Call<AttentionBean> callCollect=proModel.userCollectMatch(u_id, m_id);
        callCollect.enqueue(new Callback<AttentionBean>() {
            @Override
            public void onResponse(Call<AttentionBean> call, Response<AttentionBean> response) {
                if ("yes".equals(coresult)) {
                    Toast.makeText(MatcthDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.ic_collect_hui);
                }
                else{
                    Toast.makeText(MatcthDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
                refresh();
            }


            @Override
            public void onFailure(Call<AttentionBean> call, Throwable t) {

            }


        });
    }


    private void refresh(){
        isBeCollected();
    }
}

